/**
 * 
 */
package com.ntv.persistent.obituary;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import com.ntv.domain.IObituary;
import com.ntv.domain.Obituary;
import com.ntv.persistent.AbstractDao;
import com.ntv.persistent.PersistentException;

/**
 * @author Kavi
 * @Since 04/02/2009
 */
public class ObituaryDao extends AbstractDao implements IObituaryDao {
	private SimpleJdbcInsert jdbcInsert = null;
	interface CONST{
		interface QUERY{
			String RECENT_OBITUARIES = "select id," +
					" age," +
					" parent_name," +
					" native_place," +
					" date_of_death," +
					" funeral_detail," +
					" contact_detail," +
					" family_members_name," +
					" person_name" +
					" from obituary";
		
			String GET_OBITUARY_ID = "select id," +
					" age," +
					" parent_name," +
					" native_place," +
					" date_of_death," +
					" funeral_detail," +
					" contact_detail," +
					" family_members_name," +
					" person_name" +
					" from obituary where id=?";
		}
	}
	public List<IObituary> getRecentObituaries() throws PersistentException {
		List<IObituary> obituaryList = null;
		try
		{
			obituaryList = getNamedParameterJdbcTemplate().query(CONST.QUERY.RECENT_OBITUARIES, new HashMap(), getRowMapper());
		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
		return obituaryList;	

	}
	
	public Obituary getObituaryById(Obituary obituary) throws PersistentException {
		Obituary obituary2 = null;
		try
		{
			obituary2 = (Obituary) findById(obituary.getId());

		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
		return obituary2;	
	}

	public void saveObituary(Obituary obituary) throws PersistentException {
		
		if( obituary != null){
			try {
				jdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName(getTableName()).usingGeneratedKeyColumns("id");
				jdbcInsert.usingColumns( "age", "person_name", "date_of_death", "funeral_detail", "contact_detail", "family_members_name" , 
						"parent_name" , "native_place", "image" );
				
				Map<String, Object> parameters = new HashMap<String, Object>(10);
				parameters.put("age", obituary.getAge());
				parameters.put("person_name", obituary.getName());
				parameters.put("date_of_death", obituary.getDateOfDeath());
				parameters.put("funeral_detail", obituary.getFuneralDetails());
				parameters.put("contact_detail", obituary.getContactDetails());
				parameters.put("family_members_name", obituary.getFamilyMembersNames());
				parameters.put("parent_name", obituary.getParentName());
				parameters.put("native_place", obituary.getNativePlace());
				parameters.put("image", obituary.getImage());
				
				jdbcInsert.executeAndReturnKey(parameters);
				
			} catch (DataAccessException e) {
				throw new PersistentException(e);
			}
		}
	}

}
